package com.puruo.mindhall.config;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 * <b>CodeGenerator</b>
 * <b>Description: </b>
 * <b>@author:wangyong2@360humi.com</b>
 * <b>Date:</b> 2021/9/10 10:55
 * <b>Copyright:</b> Copyright 2017-2019 www.360humi.com Technology Co., Ltd. All rights reserved.
 * <b>Changelog:</b>
 *   Ver          Date                    Author                  Detail
 *   ----------------------------------------------------------------------
 *   1.0   2021/9/10 10:55    wangyong2@360humi.com     new file.
 * </pre>
 */
public class CodeGeneratorOracle {
    public static final String tables = "PRO_JK_AREA,PRO_JK_SERVER,PRO_JK_CAMERAS,PRO_JK_EQUIPMENT,PRO_JK_RESOURCES";
    public static final String[] prefix = new String[]{"PRO_"};
    public CodeGeneratorOracle() {
    }

    public static void main(String[] args) {
        AutoGenerator generator = new AutoGenerator();
        generator.setTemplateEngine(new FreemarkerTemplateEngine());
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
//        String path = projectPath+"/src/main/java";
//        projectPath = "F:\\nsfw\\preprod\\AutoUpdate";
        final String path = projectPath + "/JavaSource";
        gc.setOutputDir(path);
        gc.setAuthor("wangyong");
        gc.setOpen(false);
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        gc.setEnableCache(false);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setDateType(DateType.ONLY_DATE);
        generator.setGlobalConfig(gc);
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:oracle:thin:@//101.35.170.151:1521/prdb");
        dsc.setDriverName("oracle.jdbc.driver.OracleDriver");
        dsc.setUsername("puruo");
        dsc.setPassword("asLmq9c9");
        generator.setDataSource(dsc);
        PackageConfig pc = new PackageConfig();
        pc.setModuleName((String)null);
        pc.setParent("com.puruo.mindhall");
        pc.setController("web.controller");
        pc.setEntity("entity");
        pc.setMapper("mybatis");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        generator.setPackageInfo(pc);
        InjectionConfig cfg = new InjectionConfig() {
            public void initMap() {
            }
        };
        String templatePath = "/templates/mapper-oracle.xml.ftl";
        List<FileOutConfig> focList = new ArrayList();
        focList.add(new FileOutConfig(templatePath) {
            public String outputFile(TableInfo tableInfo) {
                return path+"/com/puruo/mindhall/mybatis/" + tableInfo.getEntityName() + "Mapper" + ".xml";
            }
        });
        cfg.setFileOutConfigList(focList);
        generator.setCfg(cfg);
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setEntity("templates/entity1.java");
        templateConfig.setController("templates/controller1.java");
        templateConfig.setMapper("templates/mapper1.java");
        templateConfig.setService("templates/service1.java");
        templateConfig.setServiceImpl("templates/serviceImpl1.java");
        templateConfig.setXml((String)null);
        generator.setTemplate(templateConfig);
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(false);
        strategy.setRestControllerStyle(true);
        strategy.setInclude(tables.split(","));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(prefix);
        generator.setStrategy(strategy);
        generator.setTemplateEngine(new FreemarkerTemplateEngine());
        generator.execute();
    }

}
